/*
 * DemoMapState.h
 *
 *  Created on: 2011-11-17
 *      Author: baixp
 */

#ifndef DEMOMAPSTATE_H_
#define DEMOMAPSTATE_H_
#include "Lib9.h"
#include "L9Map.h"
using namespace lib9bada;
class DemoMapState: public Lib9, public L9IState {
private:
	L9Map* pMap;
	L9Rect hero;
public:
	DemoMapState(Application* pApp);
	virtual ~DemoMapState();
	void moveHero(int off_X, int off_Y) {
		hero.moveRect(off_X, off_Y);
		//防止将主角移动到屏幕外面去了
		if (hero.Top < 0) {
			hero.moveRect(0, -hero.Top);
		}
		if (hero.Bottom > SCR_H) {
			hero.moveRect(0, SCR_H - hero.Bottom);
		}
		if (hero.Left < 0) {
			hero.moveRect(-hero.Left, 0);
		}
		if (hero.Right > SCR_W) {
			hero.moveRect(SCR_W - hero.Right, 0);
		}
	}

	void walk(int direct) {
		if (pMap == null) {
			return;
		}
		const int K_Step = 8;
		switch (direct) {
		case L9Map::K_Map_Direction_Up:
			if ((hero.Top + hero.Height() / 2) < SCR_H / 2) {
				pMap->moveMap(direct, K_Step);
				if (pMap->isInScrollingStatus()) {
					moveHero(0, -K_Step);
				} else {
					moveHero(0, -1); //在滚动地图的过程中仍然小步移动主角
				}
			} else {
				moveHero(0, -K_Step);
			}
			break;
		case L9Map::K_Map_Direction_Down:
			if ((hero.Top + hero.Height() / 2) > SCR_H / 2) {
				pMap->moveMap(direct, K_Step);
				if (pMap->isInScrollingStatus()) {
					moveHero(0, K_Step);
				} else {
					moveHero(0, 1); //在滚动地图的过程中仍然小步移动主角
				}
			} else {
				moveHero(0, K_Step);
			}
			break;
		case L9Map::K_Map_Direction_Left:
			if ((hero.Left + hero.Width() / 2) < SCR_W / 2) {
				pMap->moveMap(direct, K_Step);
				if (pMap->isInScrollingStatus()) {
					moveHero(-K_Step, 0);
				} else {
					moveHero(-1, 0); //在滚动地图的过程中仍然小步移动主角
				}
			} else {
				moveHero(-K_Step, 0);
			}
			break;
		case L9Map::K_Map_Direction_Right:
			if ((hero.Left + hero.Width() / 2) > SCR_W / 2) {
				pMap->moveMap(direct, K_Step);
				if (pMap->isInScrollingStatus()) {
					moveHero(K_Step, 0);
				} else {
					moveHero(1, 0); //在滚动地图的过程中仍然小步移动主角
				}
			} else {
				moveHero(K_Step, 0);
			}
			break;
		}
	}
	void Init() {
		// TODO Auto-generated method stub
	}

	void Update() {
		// TODO Auto-generated method stub
		if (pMap == null) {
			return;
		}
		//按下方向键后将移动主角就实现了地图的滚动，为了简单起见，这里用移动一个矩形来代表主角
		if (isKeyPressed(K_KEY_UP)) {
			walk(L9Map::K_Map_Direction_Up);
		}
		if (isKeyPressed(K_KEY_DOWN)) {
			walk(L9Map::K_Map_Direction_Down);
		}
		if (isKeyPressed(K_KEY_LEFT)) {
			walk(L9Map::K_Map_Direction_Left);
		}
		if (isKeyPressed(K_KEY_RIGHT)) {
			walk(L9Map::K_Map_Direction_Right);
		}
	}

	void Paint() {
		// TODO Auto-generated method stub
		if (pMap == null) {
			return;
		}
		//绘制地图
		pMap->updateMap(pFG);
		//绘制地图上的物品
		pMap->updateMapObjects(pFG);

		//绘制主角,为了简单实用一个矩形来代表主角，注意在绘制矩形的时候和j2me中的差异
		pFG->SetForegroundColor(Color(0xFFFF0000));
		pFG->FillRectangle(Color(0xFFFF0000),Rectangle(hero.Left, hero.Top, hero.Width(), hero.Height()));
	}
};

#endif /* DEMOMAPSTATE_H_ */
